package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.constant.BspsTreeNodeConstant;
import cc.rengu.igas.bsps.common.dao.BspsMchntBaseInfoMapper;
import cc.rengu.igas.bsps.common.dao.BspsUserInfoMapper;
import cc.rengu.igas.bsps.common.dao.EventInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsMchntBaseInfoMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.BspsUserInfoMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.EventInfoMapperImpl;
import cc.rengu.igas.bsps.common.entity.EventInfo;
import cc.rengu.igas.bsps.common.entity.MchntBaseInfo;
import cc.rengu.igas.bsps.common.entity.UserInfo;
import cc.rengu.igas.bsps.common.enums.EventStatusEnum;
import cc.rengu.igas.bsps.common.enums.EventTypeEnum;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.request.EventDeclareRequest;
import cc.rengu.igas.bsps.facade.response.EventDeclareResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.RandomUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 事件申报
 * Created by 王成 on 2018/10/8.
 */
public class EventDeclareService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        EventDeclareRequest eventDeclareRequest = new EventDeclareRequest();
        ConvertUtil.convertOutput(eventDeclareRequest);
        return eventDeclareRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        EventDeclareRequest eventDeclareRequest = (EventDeclareRequest) request;
        if (null == eventDeclareRequest.getEventDeclareType() || eventDeclareRequest.getEventDeclareType().isEmpty()) {
            rglog.error("事件申报类型<{}>不能为空!", eventDeclareRequest.getEventDeclareType());
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        /* 登记事件申报信息 */
        EventInfoMapper eventInfoMapper = new EventInfoMapperImpl();
        EventInfo eventInfo = new EventInfo();
        eventInfo.setInstId(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID));
        eventInfo.setEventId(RandomUtil.getUUID());
        eventInfo.setEventSrc(eventDeclareRequest.getHeader().getChanlId());
        eventInfo.setEventType(eventDeclareRequest.getEventDeclareType());
        eventInfo.setDeclareDate(DateUtil.getCurrentDate());
        eventInfo.setDeclareTime(DateUtil.getCurrentTime());
        eventInfo.setDeclareSeqNum(eventDeclareRequest.getHeader().getTraceNo());
        eventInfo.setDeclareUser(eventDeclareRequest.getUserId());

        BspsUserInfoMapper bspsUserInfoMapper = new BspsUserInfoMapperImpl();
        UserInfo userInfo = bspsUserInfoMapper.selectBspsUserInfoByUserId(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID), eventDeclareRequest.getUserId());
        if (null == userInfo) {
            rglog.error("查询用户信息失败!法人行:<{}>，用户ID:<{}>", xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID), eventDeclareRequest.getUserId());
            throw new BizException(RespCodeEnum.USER_EXIST_ERROR.getRespCode(), RespCodeEnum.USER_EXIST_ERROR.getRespDesc());
        }
        eventInfo.setDeclarePhone(userInfo.getMobileNo());
        eventInfo.setEventTitle(eventDeclareRequest.getEventTitle());
        eventInfo.setEventDesc(eventDeclareRequest.getEventDesc());

        if (eventDeclareRequest.getCredentialImageInfo() != null && eventDeclareRequest.getCredentialImageInfo().size() > 0) {
            eventDeclareRequest.setCredentialImageInfo(strToList(eventDeclareRequest.getCredentialImageInfo().get(0)));
            if (eventDeclareRequest.getCredentialImageInfo().size() == 1) {
                eventInfo.setVoucherImagePath1(eventDeclareRequest.getCredentialImageInfo().get(0));
            } else if (eventDeclareRequest.getCredentialImageInfo().size() == 2) {
                eventInfo.setVoucherImagePath1(eventDeclareRequest.getCredentialImageInfo().get(0));
                eventInfo.setVoucherImagePath2(eventDeclareRequest.getCredentialImageInfo().get(1));
            } else {
                eventInfo.setVoucherImagePath1(eventDeclareRequest.getCredentialImageInfo().get(0));
                eventInfo.setVoucherImagePath2(eventDeclareRequest.getCredentialImageInfo().get(1));
                eventInfo.setVoucherImagePath3(eventDeclareRequest.getCredentialImageInfo().get(2));
            }
        }
        if (null == eventDeclareRequest.getEventPriority()) {
            eventInfo.setEventPriority("00");
        } else {
            eventInfo.setEventPriority(eventDeclareRequest.getEventPriority());
        }
        eventInfo.setEventStatus(EventStatusEnum.INIT.getEventStatus());
        eventInfo.setRemark3(eventDeclareRequest.getEventDeclareInfo());
        /* 基于事件申报类型，解析事件申报信息 */
        JSONObject eventObject = JSON.parseObject(eventDeclareRequest.getEventDeclareInfo());
        if (EventTypeEnum.TERM_FAULT.getEventType().equals(eventDeclareRequest.getEventDeclareType())) {
            /* 终端故障申报 */
            eventInfo.setMchntNo(eventObject.getString(BspsTreeNodeConstant.MCHNT_NO));
            eventInfo.setTermNo(eventObject.getString(BspsTreeNodeConstant.TERM_NO));
            eventInfo.setEventDesc(eventDeclareRequest.getEventDesc());
        } else if (EventTypeEnum.CONSUMABLE.getEventType().equals(eventDeclareRequest.getEventDeclareType())) {
            /* 耗材申报 */
            eventInfo.setMchntNo(eventObject.getString(BspsTreeNodeConstant.MCHNT_NO));
            eventInfo.setEventDesc(eventDeclareRequest.getEventDesc());
        } else if (EventTypeEnum.ABNORMAL_TXN.getEventType().equals(eventDeclareRequest.getEventDeclareType())) {
            /* 工单申报 */
            eventInfo.setMchntNo(eventObject.getString(BspsTreeNodeConstant.MCHNT_NO));
            eventInfo.setTermNo(eventObject.getString(BspsTreeNodeConstant.TERM_NO));
            eventInfo.setOrigTransSeqNum(eventObject.getString(BspsTreeNodeConstant.ORIG_TRANS_SEQ_NUM));
            eventInfo.setEventDesc(eventDeclareRequest.getEventDesc());
        } else if (EventTypeEnum.ERROR_STATE.getEventType().equals(eventDeclareRequest.getEventDeclareType())) {
            /* 差错申述 */
            eventInfo.setMchntNo(eventObject.getString(BspsTreeNodeConstant.MCHNT_NO));
            eventInfo.setOrigTransSeqNum(eventObject.getString(BspsTreeNodeConstant.ORIG_TRANS_SEQ_NUM));
            eventInfo.setEventDesc(eventDeclareRequest.getEventDesc());
        } else if (EventTypeEnum.OTHER.getEventType().equals(eventDeclareRequest.getEventDeclareType())) {
            /* 其它申报 */
            eventInfo.setMchntNo(eventObject.getString(BspsTreeNodeConstant.MCHNT_NO));
            eventInfo.setEventDesc(eventDeclareRequest.getEventDesc());
        } else {
            rglog.error("不支持的事件申报类型<{}>", eventDeclareRequest.getEventDeclareType());
            throw new BizException(RespCodeEnum.NOT_SUPPORT_ORT_TYPE.getRespCode(), RespCodeEnum.NOT_SUPPORT_ORT_TYPE.getRespDesc());
        }
        /* 事件第一处理人分配 */
        if (null != eventInfo.getMchntNo()) { /* 将工单分配置给商户经理 */
            BspsMchntBaseInfoMapper mchntInfoMapper = new BspsMchntBaseInfoMapperImpl();
            MchntBaseInfo mchntInfo = mchntInfoMapper.selectBspsMchntBaseInfoByPrimaryKey(eventDeclareRequest.getHeader().getInstId(), eventInfo.getMchntNo());
            if (null != mchntInfo && null != mchntInfo.getManagerId()) {
                eventInfo.setCurrentProcUser(mchntInfo.getManagerId());
            } else {  /* 分配给默认管理员 */
                rglog.error("无拓展经理");
                throw new BizException(RespCodeEnum.INSERT_EVENT_INFO_ERROR.getRespCode(), RespCodeEnum.INSERT_EVENT_INFO_ERROR.getRespDesc());
            }
        } else {
            rglog.error("无拓展经理");
            throw new BizException(RespCodeEnum.INSERT_EVENT_INFO_ERROR.getRespCode(), RespCodeEnum.INSERT_EVENT_INFO_ERROR.getRespDesc());
        }
        int iReturnCode = eventInfoMapper.insertEventInfo(eventInfo);
        if (0 != iReturnCode) {
            rglog.error("登记事件申报信息失败,iReturnCode:<{}>", iReturnCode);
            throw new BizException(RespCodeEnum.INSERT_EVENT_INFO_ERROR.getRespCode(), RespCodeEnum.INSERT_EVENT_INFO_ERROR.getRespDesc());
        }
        /* 组装响应信息 */
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        EventDeclareResponse eventDeclareResponse = new EventDeclareResponse();
        eventDeclareResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        eventDeclareResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        eventDeclareResponse.setEventId(eventInfo.getEventId());
        bizResponse.setResult(eventDeclareResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        EventDeclareResponse eventDeclareResponse = (EventDeclareResponse) bizResponse.getResult();
        ConvertUtil.convertInput(eventDeclareResponse);
    }

    public static List<String> strToList(String str) {
        str = str.replace("]", "").replaceAll("\\[", "").replaceAll("\"", "");
        String[] a = str.split(",");
        List<String> list = new ArrayList<String>(Arrays.asList(a));
        return list;
    }


}
